<?php
/**
 * V6
 *
 */
class data_save_folder extends data {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 添加一个用户存档文件夹
	 *
	 * @param int $uid
	 * @param string $title				// 没有转义
	 * @param string $privacy
	 * @return int
	 */
	public function add ($uid, $title, $privacy) {
		$uid 	= intval($uid);
		$title 	= addslashes(strip_tags(trim($title)));
		$privacy = ($privacy === 'private') ? 'private' : 'public';
		
		if ($uid and $title) {
			$insert_id = $this->db->execute('
				insert into
					save_folder
				set
					title			= "'.$title.'",
					user_id			= "'.$uid.'",
					privacy			= "'.$privacy.'",
					total_article	= 0
			');
			
			return $insert_id;
		} else return false;
	}
	/**
	 * 删除存档文件夹
	 *
	 * @param int $uid
	 * @param int $folder_id
	 * @return bool
	 */
	public function del ($uid, $folder_id) {
		$uid		= intval($uid);
		$folder_id	= intval($folder_id);
		
		if ($uid and $folder_id) {
			// 检查权限
			$folder = $this->db->execute('
				select
					*
				from
					save_folder
				where
					id			= "'.$folder_id.'"	and
					user_id		= "'.$uid.'"
			');
			if ($folder) {
				// 复制folder下全部文章到del表中
				$copy = $this->db->execute('
					insert into
						save_article_del
					(id, user_id, folder_id, url_id, title, dt, `status`, dt_run)
						select
							id, user_id, folder_id, url_id, title, dt, 0, "0000-00-00 00:00:00"
						from
							save_article
						where
							folder_id = "'.$folder_id.'"
				');
				// 删除文章
				$del_article = $this->db->execute('
					delete from
						save_article
					where
						folder_id = "'.$folder_id.'"
				');
				// 删除folder
				$del_folder = $this->db->execute('
					delete from 
						save_folder
					where
						id			= "'.$folder_id.'"
				');
				if ($del_folder) {		// 删除folder成功后
					// Event:删除的文档会自动在del article表中处理，不用额外发布event了
					
					return $del_article or $del_folder;
				} else return false;
			} else return false;
		} else return false;
	}
	/**
	 * 修改用户存档文件夹
	 * 		改为private时：
	 * 			删除订阅源
	 * 			删除订阅的文章
	 * 			删除所有feed文章和feed订阅
	 * 			删除圈子中的文章
	 * 			重新统计follower总数
	 *
	 * @param int $uid
	 * @param int $folder_id
	 * @param string $title			// 没有转义
	 * @param string $privacy
	 * @return bool
	 */
	public function edit ($uid, $folder_id, $title, $privacy) {
		$uid			= intval($uid);
		$folder_id		= intval($folder_id);
		$title			= addslashes(strip_tags(trim($title)));
		$privacy		= ($privacy === 'private') ? $privacy : 'public';
		
		if ($uid and $folder_id and $title) {		// 用户本人，允许修改
			$folder = $this->is_owner($uid, $folder_id);
			if ($folder) {
				$edit = $this->db->execute('
					update 
						`save_folder` 
					set 
						title			= "'.$title.'",
						privacy			= "'.$privacy.'"
					where
						id	= "'.$folder_id.'"
				');
				if ($folder['privacy'] == 'public' and $privacy == 'private') {			// 改为private，删除所有订阅和feed
					$data_event = new data_event();
					$event_param = array (
						'folder_id'	=> $folder_id
					);
					$data_event->add('folder_to_private', $event_param, 'low');
				}
				return $edit;
			} else return false;
		} else return false;
	}
	/**
	 * 获取用户的存档文件夹。
	 * 需要检查用户身份，如果不是本人，则列出public/friend权限的文件夹
	 *
	 * @param int $uid					// 被列表的用户
	 * @param string $relation			// 执行操作的用户与被列表用户的关系
	 * @return array = array (
	 * 		array (id, user_id, title, description, privacy)
	 * )
	 */
	public function ls ($uid, $relation) {
		$uid		= intval($uid);
		$relation	= ($relation === 'private') ? $relation : 'public';
		
		if ($uid) {
			$execute = $this->db->execute('
				select 	
					* 
				from 
					save_folder
				where 
					user_id 	= 	"'.$uid.'"
					'.(($relation === 'private') ? '' : ' and privacy = "public" ').'
				order by 
					id DESC
			');
			return $execute;
		} else return false;
	}
	/**
	 * 一个文件夹的详情
	 * 		验证权限
	 *
	 * @param int $uid
	 * @param int $folder_id
	 * @return array
	 */
	public function one ($uid, $folder_id) {
		$uid		= intval($uid);
		$folder_id	= intval($folder_id);
		
		if ($uid and $folder_id) {
			$execute = $this->db->execute('
				select 
					*
				from 
					save_folder
				where 
					id 			= "'.$folder_id.'"					and
					(privacy = "public" or user_id = "'.$uid.'")
			');
			if ($execute[0]) {
				return $execute[0];
			} else return false;
		} else return false;
	}
	/**
	 * 更新存档文件夹中的文章总数
	 *
	 * @param int $folder_id
	 * @param int $number
	 * @return bool
	 */
	public function count_article ($folder_id, $number=0) {
		$folder_id = intval($folder_id);
		
		if ($folder_id) {
			if ($number > 0) {
				$update = $this->db->execute('
					update
						save_folder
					set
						total_article = total_article + '.$number.'
					where
						id = "'.$folder_id.'"
				');
			} elseif ($number < 0) {
				$update = $this->db->execute('
					update
						save_folder
					set
						total_article = total_article - '.abs($number).'
					where
						id = "'.$folder_id.'"
				');
			} else {
				$update = $this->db->execute('
					update
						save_folder
					set
						total_article = (select COUNT(*) from save_article where folder_id = "'.$folder_id.'")
					where
						id = "'.$folder_id.'"
				');
			}
			return $update;
		} else return false;
	}
	/**
	 * 是否是folder的拥有者
	 *
	 * @param int $uid
	 * @param int $id
	 * @return Array
	 */
	public function is_owner ($uid, $id) {
		$uid	= intval($uid);
		$id		= intval($id);
		
		if ($uid and $id) {
			$execute = $this->db->execute('
				select
					*
				from
					save_folder
				where
					id		= "'.$id.'"		and
					user_id	= "'.$uid.'"
			');
			if ($execute) {
				return $execute[0];
			} else return false;
		} else return false;
	}
	
	
	// 以下暂时未用到
	/**
	 * 热门的存档文件夹
	 *
	 * @param int $uid
	 * @param int $page
	 * @param int $limit
	 * @return array
	 */
	public function ls_hot_followed ($uid, $page=1, $limit=10) {
		$uid	= intval($uid);
		$page	= intval($page);
		$page	= ($page) ? $page : 1;
		$limit 	= intval($limit);
		$limit 	= ($limit) ? $limit : 1;
		
		if ($uid and $page and $limit) {
			$execute = $this->db->execute('select
												save_folder.id					AS id,
												save_folder.title				AS title,
												save_folder.user_id			AS user_id,
												save_folder.total_follower		AS total_follower,
												save_folder.total_article		AS total_article,
												users.nickname					AS user_nickname
											from
												save_folder, users
											where
												save_folder.privacy 	= "public"					and
												save_folder.user_id 	<> "'.$uid.'"				and
												users.id				= save_folder.user_id
											order by
												save_folder.total_follower DESC, 
												save_folder.total_article DESC, 
												save_folder.id DESC
											limit
												'.(($page-1)*$limit).', '.$limit.'
										');
			return $execute;
		} else return false;
	}
	/**
	 * 好友关注的存档文件夹
	 *
	 * @param int $uid
	 * @param int $page
	 * @param int $limit
	 * @return array
	 */
	public function ls_friend_followed ($uid, $page=1, $limit=10) {
		$uid	= intval($uid);
		$page	= intval($page);
		$page	= ($page) ? $page : 1;
		$limit 	= intval($limit);
		$limit 	= ($limit) ? $limit : 1;
		
		if ($uid and $page and $limit) {
			$execute = $this->db->execute('select
												save_folder.id					AS id,
												save_folder.title				AS title,
												save_folder.user_id				AS user_id,
												save_folder.total_follower		AS total_follower,
												save_folder.total_article		AS total_article,
												users.nickname					AS user_nickname
											from
												friend, follow_source, save_folder, users
											where
												friend.user_id					= "'.$uid.'"					and
												follow_source.user_id			= friend.friend_id				and
												follow_source.source_type		= "follow_folder"				and
												save_folder.id					= follow_source.source_id		and
												save_folder.user_id			<> "'.$uid.'"						and
												save_folder.privacy 			= "public"						and
												users.id						= save_folder.user_id
											group by
												follow_source.source_id
											order by
												save_folder.total_follower DESC, 
												save_folder.total_article DESC, 
												save_folder.id DESC
											limit
												'.(($page-1)*$limit).', '.$limit.'
										');
			return $execute;
		} else return false;
	}
}
?>